package com.sls.api.game.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.UUID;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;

import com.sls.api.game.constant.Constant;
import com.sls.api.game.pojo.WogreatInfo;
import com.sls.api.util.IpUtil;
import com.sls.api.util.PropertiesUtil;
import com.sls.api.util.UtilMD5Encryption;
import com.sls.core.socket.Head;
import com.sls.core.socket.SocketDTO;

public class WogreatTradeStatusServlet extends SuperServlet {

	private static final long serialVersionUID = 4228975706724846750L;
	private static Logger log = Logger
			.getLogger(WogreatTradeStatusServlet.class);

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		req.setAttribute("org.eclipse.jetty.server.Request.queryEncoding",
				"UTF-8");
		resp.setContentType("text/html;charset=UTF-8");
		resp.setStatus(HttpServletResponse.SC_OK);

		String supOrderId = req.getParameter("OrderID");
		String orderId = req.getParameter("MerchantOrderID");
		String state = req.getParameter("State");
		String stateInfo = req.getParameter("StateInfo");
		String sign = req.getParameter("Sign");
		log.info("沃谷异步通知的参数：" + "OrderID：" + supOrderId + ","
				+ "MerchantOrderID：" + orderId + "," + "State：" + state + ","
				+ "StateInfo：" + stateInfo + "," + "Sign：" + sign);
		boolean flag = this.checkData(supOrderId, orderId, state, stateInfo,
				sign, resp);
		if (flag) {
			try {
				SocketDTO dto = this.sendMsg(supOrderId, orderId, state,
						stateInfo, req);
				Head head = dto.getHead();
				String resCode = head.getResponseCode();
				if (Constant.SUCCESS.equals(resCode)) {
					resp.getWriter().write("success");
					close(resp.getWriter());
				} else {
					resp.getWriter().write("未知错误!");
					close(resp.getWriter());
				}
			} catch (Exception e) {
				resp.getWriter().write("未知错误!");
				close(resp.getWriter());
			}
		}

	}

	private SocketDTO sendMsg(String supOrderId, String orderId, String state,
			String stateInfo, HttpServletRequest req) {
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode("020962");
		head.setIp(IpUtil.getIpAddr(req));
		head.setUserId("0");
		WogreatInfo info = new WogreatInfo();
		info.setWogreatOrderId(supOrderId);
		info.setOrderId(orderId);
		info.setState(state);
		info.setStateInfo(stateInfo);
		return super.send(head, info);
	}

	private boolean checkData(String supOrderId, String orderId, String state,
			String stateInfo, String sign, HttpServletResponse resp)
			throws IOException {
		boolean result = false;
		if (StringUtils.isEmpty(supOrderId)) {
			resp.getWriter().write("缺少必要参数[OrderID]!");
			close(resp.getWriter());
			return false;
		} else if (StringUtils.isEmpty(orderId)) {
			resp.getWriter().write("缺少必要参数[MerchantOrderID]!");
			close(resp.getWriter());
			return false;
		} else if (StringUtils.isEmpty(state)) {
			resp.getWriter().write("缺少必要参数[state]!");
			close(resp.getWriter());
			return false;
		} else if (StringUtils.isEmpty(sign)) {
			resp.getWriter().write("缺少必要参数[sign]!");
			close(resp.getWriter());
			return false;
		} else {
			String data = supOrderId + orderId + state;
			String md5key = PropertiesUtil.getValue("wogreat.md5.key");
			String mySign = UtilMD5Encryption.createMD5EncryptionOfUTF8(data
					+ md5key);
			if (sign.equals(mySign)) {
				result = true;
			} else {
				resp.getWriter().write("签名验证失败!");
				close(resp.getWriter());
				return false;
			}
		}
		return result;
	}

	/**
	 * 关闭资源
	 * 
	 * @param pw
	 */
	private void close(PrintWriter pw) {
		if (pw != null) {
			pw.flush();
			pw.close();
		}
	}
}
